package com.evidencer.demo.servlet;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Title: ResponseDemo.java
 * @Package com.evidencer.demo.servlet
 * @Description: TODO
 * @author Sunbeam lyzpoq_gmail_com
 * @date 2014-2-3 上午11:42:47
 * @version V1.0
 */
@WebServlet(name="ResponseDemo",urlPatterns="/ResponseDemo")
public class ResponseDemo extends HttpServlet {

	/*
	*
	
	Title: doGet
	
	*
	
	Description: 
	
	* @param req
	* @param resp
	* @throws ServletException
	* @throws IOException
	* @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	*/
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//bytesTest(resp);
		//charsTest(resp);
		//downloadTest(resp);
		String ran=new Random().nextInt(10000000)+"";
		resp.setHeader("refresh", "3");
		resp.getWriter().write(ran);
	}

	private void downloadTest(HttpServletResponse resp)
			throws FileNotFoundException, IOException {
		String path=this.getServletContext().getRealPath("/download/1.jpg");
		String fileName=path.substring(path.lastIndexOf("\\")+1);
		System.out.println(fileName);
		resp.setContentType("image/jpeg");
		//resp.setHeader("Content-disposition", "attachment;filename="+fileName);
		InputStream in=null;
		OutputStream out=null;
		try{
			in=new FileInputStream(path);
			out=resp.getOutputStream();
			byte[] buf=new byte[1024];
			int len=0;
			while((len=in.read(buf))>0){
				out.write(buf,0,len);
			}
		}finally{
			if(in!=null){
				in.close();
			}
		}
	}

	private void charsTest(HttpServletResponse resp) throws IOException {
		String s="你好!";
		//resp.setCharacterEncoding("gb2312");
		resp.setContentType("text/html;charset=UTF-8");
		PrintWriter pw=resp.getWriter();
		pw.write(s);
		System.out.println("Success!");
	}

	private void bytesTest(HttpServletResponse resp) throws IOException,
			UnsupportedEncodingException {
		String s="你好!";
		resp.setContentType("text/html;charset=gb2312");
		OutputStream out=resp.getOutputStream();
		out.write(s.getBytes("UTF-8"));
	}

	/*
	*
	
	Title: doPost
	
	*
	
	Description: 
	
	* @param req
	* @param resp
	* @throws ServletException
	* @throws IOException
	* @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	*/
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doPost(req, resp);
	}

}

